package org.datastructure.secondday.search;

public class InsertValueSearch {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};

        int i = insertValueSearch(arr, 2, 0, arr.length-1);
        System.out.println(i);
    }

    public static int insertValueSearch(int[] arr, int key, int left, int right) {
        if (left > right || key < arr[left] || key > arr[right]) {
            return -1;
        }
        int mid = left + (right - left) / (arr[right] - arr[left]) * (key - arr[left]);
        if (key < arr[mid]) {
            return insertValueSearch(arr, key, left, mid - 1);
        } else if (key > arr[mid]) {
            return insertValueSearch(arr, key, mid + 1, right);
        } else {
            return mid;
        }
    }
}
